package com.xeem.core.utils
{
	import com.xeem.collections.HashMap;
	import com.xeem.events.NotifyEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	public class NotifyUtil
	{
		private static var map:HashMap=new HashMap();
		
		public function NotifyUtil()
		{
			
		}
		
		//派发通知事件
		public static function dispatchNotifyEvent(objName:String,event:NotifyEvent){
			var  dispatch:NotifyModel;			
			dispatch=getDisplayObject(objName);
			trace("dispatchNotifyEvent:"+event.type);
			trace("dispatchNotifyEvent:dispatch.num=>"+dispatch.num);
			trace("dispatchNotifyEvent:dispatch.maxnum=>"+dispatch.maxNum);
			dispatch.dispatchEvent(event);
			if (event.type==NotifyEvent.COUNTINUE){				
				dispatch.num--;
				if (dispatch.num==0){
					dispatch.dispatchEvent(new NotifyEvent(NotifyEvent.COMPLETE,event.data));
					dispatch.num=dispatch.maxNum;
				}			
			}
		}
		
		//派发继续事件
		public static function dispatchCountinue(objName:String,$data:*=null){
			var  dispatch:NotifyModel;			
			dispatch=getDisplayObject(objName);
			dispatch.dispatchEvent(new NotifyEvent(NotifyEvent.COUNTINUE,$data));
		}
		
		
		//监听通知事件
		public static function addNotifyEventListener(objName:String,event:String,f:Function){
			var  dispatch:NotifyModel;
			dispatch=getDisplayObject(objName);
			dispatch.addEventListener(event,f);
			if (event==NotifyEvent.NOTICE){
				dispatch.maxNum++;
				dispatch.num++;
			}
		}
		
	
		private static function getDisplayObject(objName:String):NotifyModel {
			if (!map.containsKey(objName))
				createDisplayObject(objName);
			var obj:NotifyModel;
			obj=map.get(objName) as NotifyModel;			
			return obj;
		}
		
		private static function createDisplayObject(objName:String):void {
			trace(objName);
			var notify:NotifyModel=new NotifyModel();
			map.put(objName,notify);
		}
		
	}
	
}